import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yinshui_caiying/widgets/border_dashed.dart';

///单元格带输入框小组件
///
///[title]左侧标题,*必填
///
///[placeholder]输入提示
///
///[controller]输入框控制器，当初始化时要反填值时用
///
///[onChanged]值改变的回调函数
///
///[inputFormatters]格式化函数
///
///[keyboardType]软件盘弹起的类型
///
///[unit]输入框后面的单位，比如 m、元
///
///[request]是否必填，是的话表单验证时会以弹窗的形式提示placeholder的内容
///
///[validator]验证规则，想用自定义验证规则的话，就传入
///[hasBorder]是否展示下划线，默认true
class FormTextArea extends StatelessWidget {
  String title;
  String placeholder;
  TextEditingController? controller;
  void Function(String)? onChanged;
  List<TextInputFormatter>? inputFormatters;
  TextInputType? keyboardType;
  String unit;
  int maxLength;
  FocusNode focusNode = FocusNode();
  final textLength = 0.obs;
  String? Function(String?)? validator;
  bool hasBorder;

  bool request;
  FormTextArea(
      {this.title = '',
      this.controller,
      this.onChanged,
      this.inputFormatters,
      this.keyboardType,
      this.unit = '',
      this.placeholder = '请输入内容',
      this.validator,
      this.maxLength = 100,
      this.hasBorder = true,
      this.request = false});

  @override
  Widget build(BuildContext context) {
    if (controller == null) {
      controller = TextEditingController.fromValue(TextEditingValue(
          text: '',
          selection: TextSelection.fromPosition(TextPosition(
              affinity: TextAffinity.downstream, offset: textLength.value))));
    }
    controller!.addListener(() {
      textLength.value = controller!.text.length;
    });
    focusNode.addListener(() {
      if (focusNode.hasFocus) {
        //有焦点
        controller!.selection = TextSelection.fromPosition(TextPosition(
            affinity: TextAffinity.downstream, offset: textLength.value));
      } else {
        //失去焦点
      }
    });
    return GestureDetector(
        onTap: () {
          FocusScope.of(context).requestFocus(focusNode);
        },
        child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          title != ''
              ? Container(
                  margin: EdgeInsets.only(top: 50.h),
                  child: Text(title,
                      textAlign: TextAlign.left,
                      style: TextStyle(
                        fontSize: 40.sp,
                        fontWeight: FontWeight.w400,
                      )))
              : Container(),
          Container(
            alignment: Alignment.topLeft,
            margin: EdgeInsets.only(top: 50.h, bottom: 50.h),
            height: 300.h,
            child: TextFormField(
                maxLines: 5,
                minLines: 5,
                maxLength: maxLength,
                focusNode: focusNode,
                keyboardType: keyboardType,
                inputFormatters: inputFormatters,
                onChanged: onChanged,
                controller: controller,
                textAlignVertical: TextAlignVertical.top,
                style: TextStyle(
                  color: Color.fromRGBO(193, 192, 192, 1),
                  fontSize: 34.sp,
                  fontWeight: FontWeight.w400,
                ),
                decoration: InputDecoration(
                  hintText: placeholder,
                  hintStyle: TextStyle(color: Color.fromRGBO(193, 192, 192, 1)),
                  isDense: true,
                  errorStyle: TextStyle(height: 0),
                  filled: true,
                  fillColor: Color.fromRGBO(240, 242, 245, 1),
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.all(Radius.circular(10)),
                    borderSide: BorderSide(
                      color: Color.fromRGBO(240, 242, 245, 1),
                      width: 0,
                    ),
                  ),
                  enabledBorder: OutlineInputBorder(
                    borderSide: BorderSide(
                      color: Color.fromRGBO(240, 242, 245, 1),
                      width: 0,
                    ),
                  ),
                ),
                validator: this.request
                    ? (value) {
                        if (value == '') {
                          SmartDialog.showToast(this.placeholder,
                              alignment: Alignment.center);
                          return '';
                        } else {
                          return null;
                        }
                      }
                    : null),
          ),
          hasBorder ? MySeparator() : Container()
        ]));
  }
}
